<template>
  <div class="layout-default">
    <!-- 左侧导航栏 -->
    <aside class="sidebar">
      <div class="sidebar-header">
        <div class="logo">
          <span>Deploy by adnaan</span>
        </div>
      </div>

      <nav class="nav-menu">
        <router-link to="/" class="nav-item" :class="{ active: isRoute('/') }">
          <i class="fas fa-th-large"></i>
          <span>项目</span>
        </router-link>
        <router-link to="/deploy" class="nav-item" :class="{ active: isRoute('/deploy') }">
          <i class="fas fa-rocket"></i>
          <span>部署</span>
        </router-link>
        <router-link to="/settings" class="nav-item" :class="{ active: isRoute('/settings') }">
          <i class="fas fa-cog"></i>
          <span>设置</span>
        </router-link>
      </nav>

      <div class="quick-actions">
        <router-link to="/project/create" class="action-btn">
          <i class="fas fa-plus"></i>
          <span>新建项目</span>
        </router-link>
      </div>
    </aside>

    <!-- 主内容区 -->
    <main class="main-container">
      <!-- 顶部状态栏 -->
      <header class="main-header">
        <div class="header-left">
          <h2 class="page-title">{{ getPageTitle }}</h2>
        </div>
        <div class="header-right">
          <div class="theme-switch">
            <el-dropdown trigger="click" @command="handleThemeChange">
              <button class="icon-btn">
                <i class="fas fa-palette"></i>
              </button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="light">
                    <i class="fas fa-sun"></i>
                    浅色主题
                  </el-dropdown-item>
                  <el-dropdown-item command="dark">
                    <i class="fas fa-moon"></i>
                    深色主题
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>

          <div class="user-menu">
            <el-dropdown trigger="click" @command="handleCommand">
              <div class="user-info">
                <el-avatar :size="32" class="user-avatar">
                  {{ authStore.user?.username.charAt(0).toUpperCase() }}
                </el-avatar>
                <span class="username">{{ authStore.user?.username }}</span>
                <i class="fas fa-chevron-down"></i>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <i class="fas fa-user"></i>
                    个人信息
                  </el-dropdown-item>
                  <el-dropdown-item divided command="logout">
                    <i class="fas fa-sign-out-alt"></i>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </header>

      <!-- 内容区域 -->
      <div class="main-content">
        <router-view></router-view>
      </div>
    </main>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import { ElMessageBox } from 'element-plus'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

// 当前路由
const isRoute = (path) => route.path === path

// 页面标题
const getPageTitle = computed(() => {
  const routeTitles = {
    '/': '我的项目',
    '/deploy': '部署中心',
    '/settings': '系统设置',
    '/project/create': '创建项目'
  }
  return routeTitles[route.path] || ''
})

// 处理用户菜单命令
const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      router.push('/settings')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm(
          '确定要退出登录吗？',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        await authStore.logout()
        router.push('/login')
      } catch {
        // 用户取消退出
      }
      break
  }
}

// 处理主题切换
const handleThemeChange = (theme) => {
  document.documentElement.setAttribute('data-theme', theme)
  localStorage.setItem('theme', theme)
}
</script>

<style scoped>
.layout-default {
  display: flex;
  min-height: 100vh;
  background: var(--background-primary);
}

/* 侧边栏样式 */
.sidebar {
  width: 240px;
  background: var(--background-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: var(--z-index-fixed);
}

.sidebar-header {
  height: 64px;
  padding: 0 var(--spacing-lg);
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
}

.logo {
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-bold);
  background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  padding: var(--spacing-sm);
}

/* 导航菜单样式 */
.nav-menu {
  padding: var(--spacing-lg);
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  padding: var(--spacing-sm) var(--spacing-md);
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius-lg);
  margin-bottom: var(--spacing-sm);
  transition: all var(--transition-fast);
}

.nav-item:hover {
  background: var(--background-tertiary);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--primary-light);
  color: var(--primary-color);
}

.nav-item i {
  width: 20px;
  text-align: center;
}

/* 快捷操作区域 */
.quick-actions {
  padding: var(--spacing-lg);
  border-top: 1px solid var(--border-color);
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-sm);
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  background: var(--primary-color);
  color: white;
  border-radius: var(--radius-lg);
  text-decoration: none;
  transition: all var(--transition-fast);
}

.action-btn:hover {
  background: var(--primary-dark);
  transform: translateY(-1px);
}

/* 主容器样式 */
.main-container {
  flex: 1;
  margin-left: 240px;
  min-width: 0;
}

/* 顶部状态栏样式 */
.main-header {
  height: 64px;
  background: var(--glass-background);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border-color);
  padding: 0 var(--spacing-xl);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: var(--z-index-sticky);
}

.page-title {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
}

.icon-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.icon-btn:hover {
  background: var(--background-tertiary);
  color: var(--text-primary);
}

.user-info {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.user-info:hover {
  background: var(--background-tertiary);
}

.user-avatar {
  background: var(--primary-color);
  color: white;
}

.username {
  font-size: var(--font-size-sm);
  color: var(--text-primary);
}

/* 内容区域样式 */
.main-content {
  padding: var(--spacing-xl);
  min-height: calc(100vh - 64px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .main-container {
    margin-left: 0;
  }

  .username {
    display: none;
  }
}
</style> 